
<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchParam" class="demo-form-inline">
      <el-form-item label="名称">
        <el-input v-model="searchParam.name" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="fetchData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addRowVisible()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="path"
      border
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="cpu" label="CPU" />
      <el-table-column prop="memory" label="内存(G)" />
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button size="small" @click="editRowVisible(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="delRowHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        ref="pagination"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="tablePageSize"
        :current-page="tablePage"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableTotal"
        prev-text="上一页"
        next-text="下一页"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <el-dialog :visible.sync="dialogFormVisible" title="新增">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" :disabled="true" />
        </el-form-item>
        <el-form-item label="CPU" :label-width="formLabelWidth">
          <!-- <el-input v-model="form.cpu" autocomplete="off" /> -->
          <el-input-number v-model="form.cpu" :precision="2" :step="0.1" :min="0.01" :max="10" @change="setCapacityName" />
        </el-form-item>
        <el-form-item label="内存(G)" :label-width="formLabelWidth">
          <!-- <el-input v-model="form.memory" autocomplete="off" /> -->
          <el-input-number v-model="form.memory" :precision="1" :step="0.5" :min="0.01" :max="10" @change="setCapacityName" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRowHandle">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addRow, delRow, editRow } from '@/api/setting/capacity'

export default {
  data() {
    return {
      tableData: [],
      tablePageSize: 10,
      tablePage: 1,
      tableTotal: 0,
      listLoading: true,
      searchParam: {},
      dialogFormVisible: false,
      form: {
        name: '0.1核1G',
        cpu: '0.1',
        memory: '1'
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const params = Object.assign(this.searchParam, { 'page': this.tablePage, 'page_size': this.tablePageSize })
      getList(params).then(response => {
        this.tableData = response.data
        this.tableTotal = response.total
        this.listLoading = false
      })
    },
    handleCurrentChange(page) {
      this.tablePage = page
      console.log(page, this.tablePageSize)
      this.fetchData()
    },
    handleSizeChange(pagesize) {
      this.tablePageSize = pagesize
      this.fetchData()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    setCapacityName() {
      this.form.name = this.form.cpu + '核' + this.form.memory + 'G'
    },
    addRowVisible() {
      // 解决form缓存问题
      // this.form = {}
      this.dialogFormVisible = true
    },
    editRowVisible(row) {
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    delRowHandle(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 执行调用删除接口
          delRow({ id: row.id }).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addRowHandle() {
      this.dialogFormVisible = false
      console.log(this.form.id)
      if (this.form.id !== undefined) {
        editRow(this.form.id, this.form).then(response => {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
          this.fetchData()
        })
      } else {
        addRow(this.form).then(response => {
          this.$message({
            type: 'success',
            message: '添加成功'
          })
          this.fetchData()
        })
      }
    }
  }
}
</script>

<style>

</style>
